﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    //TBList="ICC_QUERYTB;VVLIEF;VKKOPF;VKPOS;";
    public partial class YGRVV20 : DocumentBase
    {

        #region=============Properties=======================
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        string Mandant = "00";
        bool IsSetUp = false;
        YGRVV20Data m_Data;

        public DataSet setResult;
        ITXEXRPT ITXEXRPT;

        #region==========CollectionViewSource=================
        SETINTEXDB SetMaster
        {
            get
            {
                return (SETINTEXDB)this.FindResource("SetMaster");
            }
        }
        SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)this.FindResource("SetEnv");
            }
        }
        SETINTEXDB SetDetail
        {
            get
            {
                return (SETINTEXDB)this.FindResource("SetDetail");
            }
        }

        CollectionViewSource vwMaster
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwMaster");
            }
        }
        DataRow _dwMaster = null;
        DataRow dwMaster
        {
            get
            {
                try
                {
                    _dwMaster = null;
                    if (vwMaster.View.CurrentPosition >= 0)
                    {
                        _dwMaster = ((DataRowView)vwMaster.View.CurrentItem).Row;
                    }
                }
                catch (Exception ex)
                {
                }
                return _dwMaster;
            }
        }
        CollectionViewSource vwDetail
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwDetail");
            }
        }
        DataRow dwDetail
        {
            get
            {
                if (vwDetail.View == null)
                    return null;
                if (vwDetail.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwDetail.View.CurrentItem).Row;
            }
        }
        string ART
        {
            get
            {
                if (dwMaster == null)
                    return "";
                return dwMaster["VKPART"].ToString();

            }
        }
        public string KRS
        {
            get
            {
                return m_Data.Right("  " + txtVKPAUFKRS.Text.ToString().Trim(), 2);
            }
            set
            {
                txtVKPAUFKRS.Text = value;
            }
        }
        public string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtVKPAUFLFD.Text.ToString().Trim(), 6); ;
            }
            set
            {
                txtVKPAUFLFD.Text = value;
            }
        }
        public string POS
        {
            get
            {
                return m_Data.Right("000000" + txtVKPAUFPOS.Text.ToString().Trim(), 3); ;
            }
            set
            {
                txtVKPAUFPOS.Text = value;
            }
        }
        string DelDate
        {
            get
            {
                if (txtDel.Text.Trim() == "")
                {
                    MessageBox.Show("请设定交期");
                }
                return Helpers.Helper.Left(txtDel.Text.ToString() + "99999999", 8).Trim();
            }
            set
            {
                txtDel.Text = value;
            }
        }

        #endregion
        BindingListCollectionView cvMaster
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgMaster.ItemsSource);
            }
        }
        #endregion
        int total = 0;
        public VVVLIEF VVVLIEF
        {
            get
            {
                return m_Data.VVVLIEF;
            }
        }
        public YGRVV20()
        {
            InitializeComponent();
            this.Name = "运输清单";
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                SetEnv.Clear();
                SetEnv.Merge(m_Data.LoadEnv());
                IsLoaded = true;
                vwMaster.View.CurrentChanged += vwMaster_CurrentChanged;
                vwEvents.View.CurrentChanged += vwEvents_CurrentChanged;
                //KRS = m_Data.year;
                //LFD = "000014"; 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }
        void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    InitializeComponent();
                }
                m_Data = new YGRVV20Data();
                ITXEXRPT = new ITXEXRPT();
                ITXEXRPT.Root = "C:\\IntexReport";
                ITXEXRPT.strCompany = app.company;
                ITXEXRPT.PrintType = "OpenExcel"; 
                DateTime now = System.DateTime.Now.AddDays(-7);
                txtDel.Text = now.Year + Helpers.Helper.Right("00" + now.Month, 2) + Helpers.Helper.Right("00" + now.Day, 2);
                txtDEnd.Text = m_Data.date;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
     
        #region========Query========

        private void QueryYGRVV20(object sender, RoutedEventArgs e)
        {
            DataSet SResult = new DataSet();
            try
            {
                //搜索
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    SResult = m_Data.BuildData(app.Mandant, IsChkVV20, txtDel.Text, txtDEnd.Text);
                    SetMaster.Clear();
                    SetMaster.Merge(SResult);
                    if (vwMaster.View != null)
                    {
                        vwMaster.View.MoveCurrentToFirst();
                    }
                    if (vwVKKOPF.View != null)
                    {
                        vwVKKOPF.View.MoveCurrentToFirst();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void txtVKPAUFLFD_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter)
            {
                return;
            }
            //btnQuery_Click(null, null);
        }

        #endregion
        #region========Print========
        private void PrintYGRVV20(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet SResult = new DataSet();
                DataSet SReturn = new DataSet();
                foreach (DataRow row in SetDetail.Tables["VVLIEF"].Rows)
                {
                    SResult = VVVLIEF.QueryVV(row);
                    SReturn.Merge(SResult);
                }
                SetDetail.Merge(SReturn);
                ITXEXRPT.dsSource = SetDetail;
                ITXEXRPT.dsSource.Merge(SetEnv);
                ITXEXRPT.DocType = "YGRVV20LIST";
                ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
                string filename = ITXEXRPT.WriteExcelReport();
                SaveVV08(null, null);
                SetDetail.Clear();
                KDKNAME = "@#$";


            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        #endregion 
        string KDKNAME = "@#$";
        private void Add2List(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            try
            {
                if (KDKNAME == "@#$")
                {
                    KDKNAME = dwMaster["KDKNAME"].ToString();
                }
                string VVAVISNR = dwMaster["VVAVISNR"].ToString();
                string VVLIEFSCH = dwMaster["VVLIEFSCH"].ToString();
                string filter = "VVAVISNR='" + VVAVISNR + "' AND KDKNAME='" + KDKNAME + "'";
                string filter2 = "VVAVISNR='" + VVAVISNR + "'";
                if (ChkVV20.IsChecked == true)
                {
                    if (dwMaster["VVAVISNR"].ToString() == "00000000")
                    {
                        MessageBox.Show("必需仓库先确认出货!");
                        return;
                    }
                    filter = "VVAVISNR='" + VVAVISNR + "' AND KDKNAME='" + KDKNAME + "'";
                    filter2 = "VVAVISNR='" + VVAVISNR + "'";
                }
                else
                {
                    filter = "VVLIEFSCH='" + VVLIEFSCH + "' AND KDKNAME='" + KDKNAME + "'";
                    filter2 = "VVLIEFSCH='" + VVLIEFSCH + "'";
                }
                //                vwMaster.View.MoveCurrentTo(-1);
                DataRow[] rows = SetMaster.Tables["VVLIEF"].Select(filter);
                if (rows.Length > 0)
                {
                    DataTable tb = rows.CopyToDataTable();
                    tb.TableName = "PVVLIEF";
                    SetDetail.Merge(tb);
                    foreach (DataRow row in rows)
                    {
                        row.Delete();
                    }
                }
                else
                {
                    MessageBox.Show("必需相同客户才可以组建同一个运输清单!");
                    return;
                }
                //filter = "VVAVISNR='" + VVAVISNR + "'";
                rows = SetMaster.Tables["PVVLIEF"].Select(filter2);
                if (rows.Length > 0)
                {
                    DataTable tb = rows.CopyToDataTable();
                    tb.TableName = "VVLIEF";
                    SetDetail.Merge(tb);
                }
                vwDetail.Source = SetDetail.Tables["VVLIEF"];
                if (vwDetail.View != null)
                {
                    vwDetail.View.MoveCurrentToFirst();
                }
                SetMaster.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void ResetList(object sender, RoutedEventArgs e)
        {
            KDKNAME = "@#$";
            SetDetail.Clear();
            SetMaster.Clear();
            QueryYGRVV20(null, null);
        }

        private void SaveVV08(object sender, RoutedEventArgs e)
        {
            if (dwDetail == null)
            {
                MessageBox.Show("请选择一个发货单!");
                return;
            }
            foreach (DataRow row in SetDetail.Tables["VVLIEF"].Rows)
            {
                row["VVBZBEZU"] = VVBZBEZU.Text;///车牌号
                row["VVSEQUNO"] = VVSEQUNO.Text;///快递
                row["VVORTU"] = VVORTU.Text;//司机姓名和联系方式
                row["VVRAITING"] = "   ";
                if (ChkVV20.IsChecked == true)
                {
                    //row["VVRAITING"] = "1  ";
                }
                INTEXDBC.UpdateVVLIEF(row, 2);
            }
        }

        private void PrintQCCommand(object sender, RoutedEventArgs e)
        {
            DataSet SetQC = new DataSet(); 
            Dictionary<string, object> PM = VQORDER.Instance.PM;
            foreach (DataRow row in SetDetail.Tables["VVLIEF"].Rows)
            {
                if (AVGVV20.IsChecked==true)
                {
                    DataSet SResult = VQORDER.Instance.QueryQORDER(row, 6);
                    SetQC.Merge(SResult);
                }
                else
                {
                    DataSet SResult = VQORDER.Instance.QueryQORDER(row, 5);
                    SetQC.Merge(SResult);
                }  
            }
            cDR.DocType = "YGRQCVV";
            cDR.PRINT(SetQC);
        }
    }
}
